<?php

namespace App\Http\Validate\Article;

use App\Error\BaseError;
use App\Exceptions\KfHttpResponseException;
use App\Http\Validate\AbstractBaseValidate;
use App\Models\Article\ArticleCategoryModel;
use App\Models\Article\ArticleModel;
use App\Models\Article\ArticleRecommendModel;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ArticleRecommendValidate extends AbstractBaseValidate
{
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [];
    }

    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [];
    }

    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [];
    }

    public static function listValidate()
    {
        $params = request()->all();
        return self::getInstance()->validate(
            ["type", 'pageSize', 'pageNum', 'title'],
            $params,
            [
                "type"     => "sometimes|string",
                'pageSize' => 'int',
                'pageNum'  => 'int',
                'title'    => ''
            ],
            true,
            function ($item) {
                if (empty($item['pageSize'])) {
                    $item['pageSize'] = 0;
                }
                if (empty($item['pageNum'])) {
                    $item['pageNum'] = 10;
                }
                return $item;
            }
        );
    }

    /**
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function addValidate()
    {
        $params = request()->all();
        return self::getInstance()->validate(
            [
                "contentType",
                "effectiveType",
                "type",
                "articleId",
                "title",
                "startAt",
                "endAt",
//                "description",
                "cover",
                "sort",
                'redirectUrl',
            ],
            $params,
            [
                "contentType"   => self::intInValidateBuild(true, ArticleRecommendModel::CONTENT_TYPE_TEXT),
                "type"          => self::intInValidateBuild(true, ArticleRecommendModel::TYPE_TEXT),
                "effectiveType" => self::intInValidateBuild(true, ArticleRecommendModel::EFFECTIVE_TYPE_TEXT),
                "articleId"     => Rule::when(
                    ($params["contentType"] ?? 0) == ArticleRecommendModel::CONTENT_TYPE_CATEGORY,
                    [
                        "required",
                        "int",
                        Rule::exists((new ArticleCategoryModel())->getTable(), "id")->where("isDeleted", 0)
                    ],
                    [
//                        "required",
//                        "int",
//                        Rule::exists((new ArticleModel())->getTable(), "id")->where("isDeleted", 0)
                    ]
                ),
                "startAt"       => "exclude_unless:effectiveType," . ArticleRecommendModel::EFFECTIVE_TYPE_LIMIT . "|required|date_format:Y-m-d H:i:s",
                "endAt"         => "exclude_unless:effectiveType," . ArticleRecommendModel::EFFECTIVE_TYPE_LIMIT . "|required|date_format:Y-m-d H:i:s",
                "sort"          => '',
                "title"         => self::stringValidateBuild(false, 120, 1),
//                "description"   => self::stringValidateBuild(true, 360, 1),
                "cover"         => [
                    "required",
                    "string",
                    "url"
                ],
                'redirectUrl' => self::stringValidateBuild(false, 255, 1),
            ]
        );
    }

    /**
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function updateValidate()
    {
        $params = request()->all();
        return self::getInstance()->validate(
            [
                'id',
                "contentType",
                "effectiveType",
                "type",
                "articleId",
                "title",
                "startAt",
                "endAt",
//                "description",
                "cover",
                "sort",
                'redirectUrl',
            ],
            $params,
            [
                'id' => '',

                "contentType"   => self::intInValidateBuild(true, ArticleRecommendModel::CONTENT_TYPE_TEXT),
                "type"          => self::intInValidateBuild(true, ArticleRecommendModel::TYPE_TEXT),
                "effectiveType" => self::intInValidateBuild(true, ArticleRecommendModel::EFFECTIVE_TYPE_TEXT),
                "articleId"     => Rule::when(
                    ($params["contentType"] ?? 0) == ArticleRecommendModel::CONTENT_TYPE_CATEGORY,
                    [
                        "required",
                        "int",
                        Rule::exists((new ArticleCategoryModel())->getTable(), "id")->where("isDeleted", 0)
                    ],
                    [
//                        "required",
//                        "int",
//                        Rule::exists((new ArticleModel())->getTable(), "id")->where("isDeleted", 0)
                    ]
                ),
                "startAt"       => "exclude_unless:effectiveType," . ArticleRecommendModel::EFFECTIVE_TYPE_LIMIT . "|required|date_format:Y-m-d H:i:s",
                "endAt"         => "exclude_unless:effectiveType," . ArticleRecommendModel::EFFECTIVE_TYPE_LIMIT . "|required|date_format:Y-m-d H:i:s",

                "sort"        => '',
                "title"       => self::stringValidateBuild(false, 120, 1),
//                "description" => self::stringValidateBuild(true, 360, 1),
                "cover"       => [
                    "required",
                    "string",
                    "url"
                ],
                'redirectUrl' => self::stringValidateBuild(false, 255, 1),
            ]
        );
    }

    /**
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function patchValidate()
    {
        $params = request()->all();
        return self::getInstance()->validate(
            ['id', 'sort', 'isActivate'],
            $params,
            [
                'id'         => '',
                'isActivate' => 'int',
                'sort'       => 'int'
            ]
        );
    }

    /**
     * 详情校验
     *
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function detailValidate()
    {
        $params = request()->all();
        return self::getInstance()->validate(
            ["id"],
            $params,
            [
                "id" => [
                    "required",
                    "int",
                    Rule::exists((new ArticleRecommendModel())->getTable(), "id")
                ]
            ]
        );
    }
}
